//
//  XMCardBagListTableViewCell.m
//  XunMi
//
//  Created by MacBook on 2024/4/16.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMCardBagListTableViewCell.h"
#import "XMDiscountsModel.h"

@interface XMCardBagListTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *moneyLa;
@property (weak, nonatomic) IBOutlet UILabel *useableLa;
@property (weak, nonatomic) IBOutlet UILabel *dateLa;
@property (weak, nonatomic) IBOutlet UILabel *descLa;
@property (weak, nonatomic) IBOutlet UILabel *nameLa;
@property (weak, nonatomic) IBOutlet UILabel *typeLa;
@property (weak, nonatomic) IBOutlet UIImageView *bgImg1;
@property (weak, nonatomic) IBOutlet UIImageView *bgImg2;
@property (weak, nonatomic) IBOutlet UIImageView *leftImgV;
@property (weak, nonatomic) IBOutlet UIButton *actButton;
@property (weak, nonatomic) IBOutlet UIImageView *stateImgV;

@end

@implementation XMCardBagListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.actButton radiusWithRadius:14 corner:(UIRectCornerAllCorners)];
    self.actButton.layer.borderWidth = 1;
}

- (void)setupCellWithModel:(XMDiscountsModel *)model type:(NSInteger)type {
    self.model = model;
    self.nameLa.text = model.title;
    self.typeLa.text = model.type;
    self.leftImgV.image = type == 1 ? kGetImage(model.leftImg) : kGetImage(@"card_l_gary");
    self.bgImg1.image = type == 1 ? kGetImage(model.typeImg1) : kGetImage(@"card_gary1");
    self.bgImg2.image = type == 1 ? kGetImage(model.typeImg2) : kGetImage(@"card_gary2");
    self.descLa.text = model.describe;
    self.dateLa.text = type == 2 ? kStringFormat(@"%@", model.handle_time) : kStringFormat(@"%@到期", model.expire_time);
    
    if (model.xmModule == XMDiscountsRecharge) {
        self.useableLa.text = kStringFormat(@"满%ld可使用", model.amount);
        NSString *str = kStringFormat(@"%@%@", model.reduce, model.unitStr);
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attrStr addAttributes:@{NSFontAttributeName: kUIFontBold(30)} range:[str rangeOfString:model.reduce]];
        [attrStr addAttributes:@{NSFontAttributeName: kUIFontBold(12)} range:[str rangeOfString:model.unitStr]];
        self.moneyLa.attributedText = attrStr;
    } else {
        self.useableLa.text = kStringFormat(@"%@", model.describe);
        NSString *str = kStringFormat(@"%@%@", model.number, model.unitStr);
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attrStr addAttributes:@{NSFontAttributeName: kUIFontBold(26)} range:[str rangeOfString:model.number]];
        [attrStr addAttributes:@{NSFontAttributeName: kUIFontBold(12)} range:[str rangeOfString:model.unitStr]];
        self.moneyLa.attributedText = attrStr;
    }
    
    self.moneyLa.textColor = model.xmColor;
    [self.actButton setTitleColor:model.xmColor forState:UIControlStateNormal];
    self.actButton.layer.borderColor = model.xmColor.CGColor;
    
    if (type == 1) {
        self.nameLa.textColor = kHEX(@"#1F1F1F");
        self.useableLa.textColor = kHEX(@"959595");
        self.descLa.textColor = kHEX(@"#959595");
        self.dateLa.textColor = kHEX(@"#FE2630");
        self.moneyLa.textColor = kHEX(@"#FF4258");
        self.actButton.hidden = NO;
        self.stateImgV.hidden = YES;
    } else {
        self.nameLa.textColor = kHEX(@"#C8C8C8");
        self.useableLa.textColor = kHEX(@"#C8C8C8");
        self.descLa.textColor = kHEX(@"#C8C8C8");
        self.dateLa.textColor = kHEX(@"#C8C8C8");
        self.moneyLa.textColor = kHEX(@"#C8C8C8");
        self.actButton.hidden = YES;
        self.stateImgV.hidden = NO;
        self.stateImgV.image = kGetImage(type == 2 ? @"card_used" : @"card_lose");
    }
}
- (IBAction)useAct:(id)sender {
    if (self.useBlock) {
        self.useBlock(self.model);
    }
}

@end
